<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="JUnit4使用的基本注解JUnit4基本注释，下表列出了这些注释的概括：    注解 描述     @Test public void method() 测试注释指示该公共无效方法它所附着可以作为一个测试用例。可以指定一个异常这个已成将会捕获测试会成功，还可以指定超时时间超过指定的运行时间价将会自动判断为超时   @Before public void method() 注释表示，该方法必须在类">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/junit4使用整理（二）/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="JUnit4使用的基本注解JUnit4基本注释，下表列出了这些注释的概括：    注解 描述     @Test public void method() 测试注释指示该公共无效方法它所附着可以作为一个测试用例。可以指定一个异常这个已成将会捕获测试会成功，还可以指定超时时间超过指定的运行时间价将会自动判断为超时   @Before public void method() 注释表示，该方法必须在类">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-10-09T07:22:37.337Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="JUnit4使用的基本注解JUnit4基本注释，下表列出了这些注释的概括：    注解 描述     @Test public void method() 测试注释指示该公共无效方法它所附着可以作为一个测试用例。可以指定一个异常这个已成将会捕获测试会成功，还可以指定超时时间超过指定的运行时间价将会自动判断为超时   @Before public void method() 注释表示，该方法必须在类">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/junit4使用整理（二）/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/junit4使用整理（二）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="JUnit4使用的基本注解"><a href="#JUnit4使用的基本注解" class="headerlink" title="JUnit4使用的基本注解"></a>JUnit4使用的基本注解</h3><p>JUnit4基本注释，下表列出了这些注释的概括：</p>
<table>
<thead>
<tr>
<th>注解</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>@Test public void method()</td>
<td>测试注释指示该公共无效方法它所附着可以作为一个测试用例。可以指定一个异常这个已成将会捕获测试会成功，还可以指定超时时间超过指定的运行时间价将会自动判断为超时</td>
</tr>
<tr>
<td>@Before public void method()</td>
<td>注释表示，该方法必须在类中的每个测试之前执行，以便执行测试某些必要的先决条件</td>
</tr>
<tr>
<td>@BeforeClass public static void method()</td>
<td>BeforeClass注释指出这是附着在静态方法必须执行一次并在类的所有测试之前。发生这种情况时一般是测试计算共享配置方法(如连接到数据库)</td>
</tr>
<tr>
<td>@After public void method()</td>
<td>注释指示，该方法在执行每项测试后执行(如执行每一个测试后重置某些变量，删除临时变量等)</td>
</tr>
<tr>
<td>@AfterClass public static void method()</td>
<td>当需要执行所有的测试在JUnit测试用例类后执行，AfterClass注解可以使用以清理建立方法，(从数据库如断开连接)。注意：附有此批注(类似于BeforeClass)的方法必须定义为静态</td>
</tr>
<tr>
<td>@Ignore public static void method()</td>
<td>当想暂时禁用特定的测试执行可以使用忽略注释。每个被注解为@Ignore的方法将不被执行</td>
</tr>
</tbody>
</table>
<h4 id="一个例子"><a href="#一个例子" class="headerlink" title="一个例子"></a>一个例子</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.junit.Assert.*;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> org.junit.*;</span><br><span class="line"><span class="keyword">import</span> a_baseForJava.a_junit4.main.ItemDemo;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ItemTest</span> </span>&#123;</span><br><span class="line">	ArrayList&lt;String&gt; testList;</span><br><span class="line">	<span class="meta">@BeforeClass</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">onceExecutedBeforeAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"@BeforeClass: onceExecutedBeforeAll"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Before</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">executedBeforeEach</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		testList = <span class="keyword">new</span> ArrayList();</span><br><span class="line">		System.out.println(<span class="string">"@Before: executedBeforeEach"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSum</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		ItemDemo sum = <span class="keyword">new</span> ItemDemo();</span><br><span class="line">		assertEquals(sum.sum(<span class="number">10</span>, <span class="number">12</span>), <span class="number">22</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">EmptyCollection</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		assertTrue(testList.isEmpty());</span><br><span class="line">		System.out.println(<span class="string">"@Test: EmptyArrayList"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">OneItemCollection</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		testList.add(<span class="string">"oneItem"</span>);</span><br><span class="line">		assertEquals(<span class="number">1</span>, testList.size());</span><br><span class="line">		System.out.println(<span class="string">"@Test: OneItemArrayList"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@After</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">executedAfterEach</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		testList.clear();</span><br><span class="line">		System.out.println(<span class="string">"@After: executedAfterEach"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@AfterClass</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">onceExecutedAfterAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"@AfterClass: onceExecutedAfterAll"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Ignore</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">executionIgnored</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"@Ignore: This execution is ignored"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="控制台输出的结结果"><a href="#控制台输出的结结果" class="headerlink" title="控制台输出的结结果"></a>控制台输出的结结果</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@BeforeClass</span>: onceExecutedBeforeAll</span><br><span class="line"><span class="meta">@Before</span>: executedBeforeEach</span><br><span class="line"><span class="meta">@Test</span>: testSum</span><br><span class="line"><span class="meta">@After</span>: executedAfterEach</span><br><span class="line"><span class="meta">@Before</span>: executedBeforeEach</span><br><span class="line"><span class="meta">@Test</span>: EmptyArrayList</span><br><span class="line"><span class="meta">@After</span>: executedAfterEach</span><br><span class="line"><span class="meta">@Before</span>: executedBeforeEach</span><br><span class="line"><span class="meta">@Test</span>: OneItemArrayList</span><br><span class="line"><span class="meta">@After</span>: executedAfterEach</span><br><span class="line"><span class="meta">@AfterClass</span>: onceExecutedAfterAll</span><br></pre></td></tr></table></figure>
<blockquote>
<p>总结：对于java的类来说所有BeforeClass 会在所有的测试调用前进行调用 AfterClass 将会在所有的测试使用完成后在进行调用 Before After 将会在所有的测试完成的时候进行调用</p>
</blockquote>
<h3 id="junit断言"><a href="#junit断言" class="headerlink" title="junit断言"></a>junit断言</h3><p>这些方法都受到 Assert 类扩展了java.lang.Object类并为它们提供编写测试，以便检测故障。下表中有一种最常用的断言方法的更详细的解释</p>
<table>
<thead>
<tr>
<th>断言</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>void assertEquals([String message], expected value, actual value)</td>
<td>断言两个值相等。值可能是类型有 int, short, long, byte, char or java.lang.Object. 第一个参数是一个可选的字符串消息</td>
</tr>
<tr>
<td>void assertTrue([String message], boolean condition)</td>
<td>断言一个条件为真</td>
</tr>
<tr>
<td>void assertFalse([String message],boolean condition)</td>
<td>断言一个条件为假</td>
</tr>
<tr>
<td>void assertNotNull([String message], java.lang.Object object)</td>
<td>断言一个对象不为空(null)</td>
</tr>
<tr>
<td>void assertNull([String message], java.lang.Object object)</td>
<td>断言一个对象为空(null)</td>
</tr>
<tr>
<td>void assertSame([String message], java.lang.Object expected, java.lang.Object actual)</td>
<td>断言，两个对象引用相同的对象</td>
</tr>
<tr>
<td>void assertNotSame([String message], java.lang.Object unexpected, java.lang.Object actual)</td>
<td>断言，两个对象不是引用同一个对象</td>
</tr>
<tr>
<td>void assertArrayEquals([String message], expectedArray, resultArray)</td>
<td>断言预期数组和结果数组相等。数组的类型可能是 int, long, short, char, byte or java.lang.Object.</td>
</tr>
</tbody>
</table>
<p>在以上类中我们可以看到，这些断言方法是可以工作的。</p>
<ul>
<li>assertEquals() 如果比较的两个对象是相等的，此方法将正常返回；否则失败显示在JUnit的窗口测试将中止。</li>
<li>assertSame() 和 assertNotSame() 方法测试两个对象引用指向完全相同的对象。</li>
<li>assertNull() 和 assertNotNull() 方法测试一个变量是否为空或不为空(null)。</li>
<li>assertTrue() 和 assertFalse() 方法测试if条件或变量是true还是false。</li>
<li>assertArrayEquals() 将比较两个数组，如果它们相等，则该方法将继续进行不会发出错误。否则失败将显示在JUnit窗口和中止测试。</li>
</ul>
<h4 id="JUnit4套件测试"><a href="#JUnit4套件测试" class="headerlink" title="JUnit4套件测试"></a>JUnit4套件测试</h4><p>测试套件是一些测试不同类用例，可以使用@RunWith和@Suite注解运行所有东西在一起。如果有很多测试类，想让它们都运行在同一时间，而不是单一地运行每个测试，这是非常有用的。</p>
<p>当一个类被注解为<strong>@RunWith</strong>， JUnit 将调用被在其中注解，以便运行测试类，而不使用内置的 JUnit 运行方法。</p>
<blockquote>
<p>快速生成：包右键&gt;&gt;new&gt;&gt;JUnit test Suite&gt;&gt;finish</p>
</blockquote>
<p>指定套件类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.junit.Assert.*;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.junit.runners.Suite;</span><br><span class="line"><span class="meta">@RunWith</span>(Suite.class)</span><br><span class="line"><span class="meta">@Suite</span>.SuiteClasses(value= &#123;TestOne.class,TestTwo.class&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ItemTest</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>两个测试类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestOne</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is my test 1"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestTwo</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"this is my test 2"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出结果</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span> is my test <span class="number">1</span></span><br><span class="line"><span class="keyword">this</span> is my test <span class="number">2</span></span><br></pre></td></tr></table></figure>
<h3 id="java参数化自动测试"><a href="#java参数化自动测试" class="headerlink" title="java参数化自动测试"></a>java参数化自动测试</h3><p>一个测试类也可以被看作是一个参数化测试类要满足下列所有要求：</p>
<ul>
<li>该类被注解为 @RunWith(Parameterized.class).</li>
<li>如前一节中所说明的, @RunWith 注解让JUnit来调用其中的注释来运行测试类，代替使用内置的JUnit运行器，Parameterized 是一个在JUnit内的运行器将运行相同的测试用例组在不同的输入。</li>
<li>这个类有一个构造函数，存储测试数据。</li>
<li>这个类有一个静态方法生成并返回测试数据，并注明@Parameters注解。</li>
<li>这个类有一个测试，它需要注解@Test到方法。</li>
</ul>
<blockquote>
<p>Junit底层通过多次生成新的类进行多次测试</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.junit.Assert.*;</span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.Collection;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.junit.runners.Parameterized;</span><br><span class="line"><span class="keyword">import</span> org.junit.runners.Parameterized.Parameters;</span><br><span class="line"><span class="keyword">import</span> a_baseForJava.a_junit4.main.ItemDemo;</span><br><span class="line"><span class="meta">@RunWith</span>(Parameterized.class)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ItemTest</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> expected;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> first;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> second;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ItemTest</span><span class="params">(<span class="keyword">int</span> expectedResult, <span class="keyword">int</span> firstNumber, <span class="keyword">int</span> secondNumber)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.expected = expectedResult;</span><br><span class="line">		<span class="keyword">this</span>.first = firstNumber;</span><br><span class="line">		<span class="keyword">this</span>.second = secondNumber;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Parameters</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Collection <span class="title">addedNumbers</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> Arrays.asList(<span class="keyword">new</span> Integer[][] &#123; &#123; <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span> &#125;, &#123; <span class="number">5</span>, <span class="number">2</span>, <span class="number">3</span> &#125;, &#123; <span class="number">7</span>, <span class="number">3</span>, <span class="number">4</span> &#125;, &#123; <span class="number">9</span>, <span class="number">4</span>, <span class="number">5</span> &#125;, &#125;);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sum</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		ItemDemo add = <span class="keyword">new</span> ItemDemo();</span><br><span class="line">		System.out.println(<span class="string">"Addition with parameters : "</span> + first + <span class="string">" and "</span> + second);</span><br><span class="line">		assertEquals(expected, add.sum(first, second));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试结果</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Addition with parameters : <span class="number">1</span> and <span class="number">2</span></span><br><span class="line">Addition with parameters : <span class="number">2</span> and <span class="number">3</span></span><br><span class="line">Addition with parameters : <span class="number">3</span> and <span class="number">4</span></span><br><span class="line">Addition with parameters : <span class="number">4</span> and <span class="number">5</span></span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/maven 实战笔记 （二）maven常用插件/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/linux mint等ubuntu系统全局字体不统一，楷体无法更换问题/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#JUnit4使用的基本注解"><span class="nav-number">1.</span> <span class="nav-text">JUnit4使用的基本注解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#一个例子"><span class="nav-number">1.1.</span> <span class="nav-text">一个例子</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#控制台输出的结结果"><span class="nav-number">1.2.</span> <span class="nav-text">控制台输出的结结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#junit断言"><span class="nav-number">2.</span> <span class="nav-text">junit断言</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#JUnit4套件测试"><span class="nav-number">2.1.</span> <span class="nav-text">JUnit4套件测试</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#java参数化自动测试"><span class="nav-number">3.</span> <span class="nav-text">java参数化自动测试</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
